package com.tt.config;

import com.tt.interceptor.MyHandshakeInterceptor;
import com.tt.socket.TtWebSocketHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * lenovo
 * 配置websocket
 * 2022/3/6 - 18:31
 */
@Configuration
@EnableWebMvc
@EnableWebSocket
public class WebSocketServerConfig implements WebSocketConfigurer, WebMvcConfigurer
{
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry)
    {
        registry.addHandler(myWebSocketHandler(),"/websocket")
                .setAllowedOrigins("*")
                .addInterceptors(myHandshakeInterceptor());

        registry.addHandler(myWebSocketHandler(),"/sockjs/websocket")
                .setAllowedOrigins("*")
                .addInterceptors(myHandshakeInterceptor())
                .withSockJS();
    }

    @Bean
    public TtWebSocketHandler myWebSocketHandler()
    {
        return new TtWebSocketHandler();
    }

    @Bean
    public MyHandshakeInterceptor myHandshakeInterceptor()
    {
        return new MyHandshakeInterceptor();
    }
}
